package com.hnevc.yourhealthmanager.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "JSONTOTS")
public class JsonToTsController {

    public Integer subInterfaceIndex = 0;

    @PostMapping("/jsontots")
    @ResponseBody
    @ApiOperation(value = "generateTypeScriptInterface", notes = "通过post将jsonStr字符串转换为ts")
    public String generateTypeScriptInterface(@RequestParam String jsonStr) {
        JSONObject jsonObject = new JSONObject(jsonStr);
        StringBuilder sb = new StringBuilder("")
                .append("export interface ResponseObject {\n");
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (value instanceof JSONArray) {
                subInterfaceIndex++;
                sb.append("\t\"").append(key).append("\"?: DataItem[];\n");
                subInterfaceIndex++;
            } else if (value instanceof JSONObject) {
                subInterfaceIndex++;
                sb.append("\t\"").append(key).append("\"?: DataItem;\n");
            } else if (value instanceof Number) {
                sb.append("\t\"").append(key).append("\"?: ").append("number").append(";\n ");
            } else if (value instanceof Boolean) {
                sb.append("\t\"").append(key).append("\"?: ").append("boolean").append(";\n ");
            } else if (value instanceof String) {
                sb.append("\t\"").append(key).append("\"?: ").append("string").append(";\n ");
            }
        }
        sb.append("}\n");

        if (subInterfaceIndex > 0) {

            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                if (value instanceof JSONArray) {
                    JSONObject jsonObject1 = ((JSONArray) value).getJSONObject(0);
                    sb.append("export interface DataItem {\n");
                    for (String key2 : jsonObject1.keySet()) {
                        Object value2 = jsonObject1.get(key2);
                        if (value2 instanceof Number) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("number").append("; \n");
                        } else if (value2 instanceof Boolean) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("boolean").append("; \n");
                        } else if (value2 instanceof String) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("string").append("; \n");
                        }
                    }
                    sb.append("\n}");
                }
                if (value instanceof JSONObject) {
                    JSONObject jsonObject2 = (JSONObject) value;
                    sb.append("export interface DataItem {\n");
                    for (String key2 : jsonObject2.keySet()) {
                        Object value2 = jsonObject2.get(key2);
                        if (value2 instanceof Number) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("number").append("; \n");
                        } else if (value2 instanceof Boolean) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("boolean").append("; \n");
                        } else if (value2 instanceof String) {
                            sb.append("\t\"").append(key2).append("\"?: ").append("string").append("; \n");
                        }
                    }
                    sb.append("\n}");
                }
            }
        }
        return sb.toString();
    }
}
